package edu.ccut.saturn.studio.logic.model.drop;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.xml.loader.InformationNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.StartNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.logic.StartNodeInLogicLoader;

public abstract class LogicDropHandler extends AbstarctDropHandler {

	protected static final String LOGIC_DROP_HANDLER_TWO_DOT = "::";

	public static final String FILE_POSTFIX = ".logic";

	protected InformationNodeLoader informationNodeLoader = new InformationNodeLoader();

	protected StartNodeLoader startNodeLoader = new StartNodeLoader();

	public Shape getShape(String filePath,IProject project) {
		
		String symbolicName = super.getBundleName(project);
		Element root = null;
		try {
			root = SaturnW3CUtil.getDocument(filePath).getDocumentElement();
		} catch (XMLException e) {
			e.printStackTrace();
		}

		return getShape(root, symbolicName);
	}

	public Shape getShape(JarEntryFile jarEntryFile) throws Exception {
		InputStream in = null;

		try {
			DocumentBuilderFactory domfac = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();

			in = (InputStream) jarEntryFile.getContents();

			Document doc = dombuilder.parse(in);
			String bundleName = super.getBundleNameFromJar(jarEntryFile);
			Element root = doc.getDocumentElement();

			return this.getShape(root, bundleName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}

	/*
	 * protected Shape getShape(Element root, String symbolicName) { try {
	 * SaturnLogicNode logicNode = new SaturnLogicNode();
	 * 
	 * List<Shape> informations = informationNodeLoader.loadNodes(root);
	 * 
	 * if (informations != null && informations.size() > 0) {
	 * SaturnInformationNode informationNode = (SaturnInformationNode)
	 * informations .get(0);
	 * 
	 * String key = informationNode.getKey(); String description =
	 * informationNode.getDescription(); String name =
	 * informationNode.getName();
	 * 
	 * logicNode.setDescription(description);
	 * logicNode.setKey(symbolicName.trim() + LOGIC_DROP_HANDLER_TWO_DOT +
	 * key.trim()); logicNode.setName(name); }
	 * 
	 * List<Shape> starts = startNodeLoader.loadNodes(root);
	 * 
	 * if (starts != null && starts.size() > 0) { SaturnStartNode startNode =
	 * (SaturnStartNode) starts.get(0);
	 * logicNode.setParameters(startNode.getParameters()); }
	 * 
	 * return logicNode;
	 *  } catch (Exception e) { e.printStackTrace(); }
	 * 
	 * return null; }
	 */
	protected abstract Shape getShape(Element root, String symbolicName);
}